package com.mindata.blockchain.block.db;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.mindata.blockchain.socket.common.Const;

/**
 * rocksDB对于存储接口的实现
 * 
 * @author wuweifeng wrote on 2018/3/13.
 */
@Component
@ConditionalOnProperty("db.rocksDB")
public class RocksDbStoreImpl implements DbStore {
	@Resource
	private RocksDB rocksDB;

	@Override
	public void put(final String key, final String value) {
		try {
			rocksDB.put(key.getBytes(Const.CHARSET), value.getBytes(Const.CHARSET));
		} catch (RocksDBException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String get(final String key) {
		try {
			final byte[] bytes = rocksDB.get(key.getBytes(Const.CHARSET));
			if (bytes != null) {
				return new String(bytes, Const.CHARSET);
			}
			return null;
		} catch (final Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void remove(final String key) {
		try {
			rocksDB.delete(rocksDB.get(key.getBytes(Const.CHARSET)));
		} catch (RocksDBException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}
